package com.example.rg.generator.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.rg.generator.DTO.SetmealDto;
import com.example.rg.generator.commons.R;
import com.example.rg.generator.domain.Category;
import com.example.rg.generator.domain.Dish;
import com.example.rg.generator.domain.Setmeal;
import com.example.rg.generator.service.CategoryService;
import com.example.rg.generator.service.DishService;
import com.example.rg.generator.service.SetmealDishService;
import com.example.rg.generator.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;


    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    //添加分页
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //创建分页器
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);

        Page<Setmeal> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //条件选择器
        lqw.like(Strings.isNotEmpty(name), Setmeal::getName, name);
        //添加排序条件
        lqw.orderByAsc(Setmeal::getUpdateTime);

        setmealService.page(setmealPage, lqw);

        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");

        List<Setmeal> records = setmealPage.getRecords();
        records = records.stream().map(M -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(M, setmealDto);
            //查询分类id
            Long categoryId = M.getCategoryId();
            //通过id查找对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //通过对象载入名字
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        Page<Setmeal> Records = setmealDtoPage.setRecords(records);

        return R.success(Records);
    }

    //删除套餐数据
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("{}", ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }
    //菜品批量启售和批量停售

    /**
     * 对菜品批量或者是单个 进行停售或者是起售
     *
     * @return
     */
    @PostMapping("/status/{status}")
//这个参数这里一定记得加注解才能获取到参数，否则这里非常容易出问题
    public R<String> status(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {
        log.info("status:{}",status);
        log.info("ids:{}",ids);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null, Dish::getId, ids);
        //根据数据进行批量查询
        List<Dish> list = dishService.list(queryWrapper);

        for (Dish dish : list) {
            if (dish != null) {
                dish.setStatus(status);
                dishService.updateById(dish);
            }
        }
        return R.success("售卖状态修改成功");
    }
    //根据分条件查询套餐数据
    @GetMapping("/list")
    public R<List<Setmeal>> listR(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(lqw);

        return R.success(list);
    }

}
